/*
**************************************************************************************
*Myriad Engine                                                                       *
*Copyright (C) 2006-2007, 5d Studios (www.5d-Studios.com)                            *
*                                                                                    *
*This library is free software; you can redistribute it and/or                       *
*modify it under the terms of the GNU Lesser General Public                          *
*License as published by the Free Software Foundation; either                        *
*version 2.1 of the License, or (at your option) any later version.                  *
*                                                                                    *
*This library is distributed in the hope that it will be useful,                     *
*but WITHOUT ANY WARRANTY; without even the implied warranty of                      *
*MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU                   *
*Lesser General Public License for more details.                                     *
*                                                                                    *
*You should have received a copy of the GNU Lesser General Public                    *
*License along with this library; if not, write to the Free Software                 *
*Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA      *
**************************************************************************************
*/
package leola.frontend.parsers;

import leola.ast.ASTNode;
import leola.ast.Expr;
import leola.ast.ValDeclStmt;
import leola.ast.VarDeclStmt;
import leola.frontend.LeolaParser;
import leola.frontend.Token;
import leola.frontend.tokens.LeolaErrorCode;
import leola.frontend.tokens.LeolaTokenType;

/**
 * @author Tony
 *
 */
public class VarDeclStmtParser extends StmtParser {

	/**
	 * Is val
	 */
	private boolean isVal;
	
	/**
	 * @param parser
	 */
	public VarDeclStmtParser(LeolaParser parser) {
		this(parser, false);
	}
	
	/**
	 * @param parser
	 */
	public VarDeclStmtParser(LeolaParser parser, boolean isVal) {
		super(parser);
		this.isVal = isVal;
	}

	/* (non-Javadoc)
	 * @see leola.frontend.parsers.StmtParser#parse(leola.frontend.Token)
	 */
	@Override
	public ASTNode parse(Token t) throws Exception {
		Token token = nextToken(); // consume VAR token
		
		/* get the identifier */		
		LeolaTokenType type = token.getType(); 
		if ( ! type.equals(LeolaTokenType.IDENTIFIER)) {
			getExceptionHandler().errorToken(token, this, LeolaErrorCode.MISSING_IDENTIFIER);
		}
		
		String varName = token.getText();
		
		token = nextToken();
		if ( !token.getType().equals(LeolaTokenType.EQUALS)) {
			getExceptionHandler().errorToken(token, this, LeolaErrorCode.INVALID_ASSIGNMENT);
		}
		
		/* get the value expression */
		ExprParser expr = new ExprParser(this);
		Expr value = (Expr)expr.parse(nextToken());
		
		ASTNode varDecl = this.isVal ? new ValDeclStmt(varName, value)
						             : new VarDeclStmt(varName, value);		
		
		setLineNumber(varDecl, currentToken());
		return varDecl;
	}
}
